let marketMethod = require("../dao/marketDAO").marketMethod;
// 新增价格
exports.addMarket = function (req, res){
  let newMarket = {
    accountName: req.body.accountName,
    typeName: req.body.typeName,
    days: req.body.days,
    percent: req.body.percent,
    price : req.body.price,
    userId: req.body.userId,
    nowTime: req.body.nowTime,
  };
  marketMethod.save(newMarket, function (err){
    if(!err){
      res.send({
        code: 200,
        msg: "新增成功！"
      });
    }else{
      res.send({
        code: 201,
        msg: "新增失败！"
      });
    }
  });
};

// 获取列表
exports.getMarketList = function (req, res){
  let userId = req.body.userId
  marketMethod.findById(userId,function (err, doc) {
    if(!err){
      res.send({
        code: 200,
        data: doc,
        msg: "查询成功！"});
    }
    else{
      res.send({
        code: 201,
        msg: "查询失败！"});
    }
  })
};

// 删除
exports.delPrice = function (req, res){
  let id = req.body.id
  priceMethod.findOneById(id,function (err, doc) {
    if(!err && doc){
      let par = {
        _id: id
      }
      priceMethod.remove(par,function (err, doc) {
        if(!err){
          res.send({
            code: 200,
            msg: "删除成功！"});
        }else{
          res.send({
            code: 200,
            msg: "删除失败！"});
        }
      })
    }
  })
};

// 获取上级
exports.getSuperiorList = function (req, res){
  let userId = req.body.userId
  accountMethod.findById(userId,function (err, doc) {
    if(!err){
      res.send({
        code: 200,
        data: doc,
        msg: "查询成功！"});
    }
    else{
      res.send({
        code: 201,
        msg: "查询失败！"});
    }
  })
};

// 修改
exports.editAccount = function (req, res) {
  let newAccount = {
    id: req.body.id,
    userId: req.body.userId,
    superiorId: req.body.superiorId,
    accountName : req.body.accountName,
    account: req.body.account,
    accountPassword: req.body.accountPassword
  };
  accountMethod.findOneById(newAccount.id,function (err, doc) {
    if(!err && doc){
      accountMethod.update({'_id':newAccount.id},newAccount,function (err) {
        if(!err){
          res.send({
            code: 200,
            msg: "修改成功！"});
        }
        else{
          res.send({
            code: 201,
            msg: "修改失败！"});
        }
      })
    }
  })
}
